import json import base64 import cryptography d = str({"Payoff": "300"}) s = json.dumps(d) encodedBytes = base64.b64encode(s.encode("utf-8")) p = str(encodedBytes, "utf-8") print(p) # The Encryption Function plain_text = "540" print(plain_text) reversed_cipher = '' i = len(plain_text) - 1 while i >= 0: reversed_cipher = reversed_cipher + plain_text[i] i = i - 1 print(reversed_cipher) def cipher_encrypt(reversed_cipher, key): encrypted = "" for c in reversed_cipher: if c.isupper(): #check if it's an uppercase character c_index = ord(c) - ord('A') # shift the current character by key positions c_shifted = (c_index + key) % 26 + ord('A') c_new = chr(c_shifted) encrypted += c_new elif c.islower(): #check if its a lowecase character # subtract the unicode of 'a' to get index in [0-25) range c_index = ord(c) - ord('a') c_shifted = (c_index + key) % 26 + ord('a') c_new = chr(c_shifted) encrypted += c_new elif c.isdigit(): # if it's a number,shift its actual value c_new = (int(c) + key) % 10 encrypted += str(c_new) else: # if its neither alphabetical nor a number, just leave it like that encrypted += c return encrypted ciphertext = cipher_encrypt(reversed_cipher, 4) print(ciphertext) encodedBytes = base64.urlsafe_b64encode(ciphertext.encode("utf-8")) p = str(encodedBytes, "utf-8") print(p) # The decription function data = str(base64.urlsafe_b64decode("NDksNiBZSQ=="), "utf-8") print(data) encrypted2 = '' i = len(data) - 1 while i >= 0: encrypted2 = encrypted2 + data[i] i = i - 1 encrypted2 =str(encrypted2) print(encrypted2) def cipher_decrypt(ciphertext, key): decrypted = "" for c in ciphertext: if c.isupper(): c_index = ord(c) - ord('A') # shift the current character to left by key positions to get its original position c_og_pos = (c_index - key) % 26 + ord('A') c_og = chr(c_og_pos) decrypted += c_og elif c.islower(): c_index = ord(c) - ord('a') c_og_pos = (c_index - key) % 26 + ord('a') c_og = chr(c_og_pos) decrypted += c_og elif c.isdigit(): # if it's a number,shift its actual value c_og = (int(c) - key) % 10 decrypted += str(c_og) else: # if its neither alphabetical nor a number, just leave it like that decrypted += c return decrypted decrypted_message = cipher_decrypt(encrypted2, 4) print(decrypted_message) decodedBytes = base64.urlsafe_b64encode(decrypted_message.encode("utf-8")) p_decoded = str(decodedBytes, "utf-8") print(p_decoded) data_decoded = str(base64.urlsafe_b64decode(p_decoded), "utf-8") print(data_decoded)